<?php

/**
 * Description of gPlusBadge
 *
 * @version  1.0
 * @author Daniel Eliasson (joomla at stilero.com)
 * @copyright  (C) 2012-apr-23 Stilero Webdesign http://www.stilero.com
 * @category Plugins
 * @license    GPLv2
 *
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 *
 * This file is part of gplusbadge.
 *
 * gPlusBadge is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * gPlusBadge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gPlusBadge.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
// no direct access
defined('_JEXEC') or die('Restricted access');

// import library dependencies
jimport('joomla.plugin.plugin');

class plgContentGplusbadge extends JPlugin {

    var $config;

    function plgContentGplusbadge(&$subject, $config) {
        parent::__construct($subject, $config);
        $language = JFactory::getLanguage();
        $language->load('plg_content_gplusbadge', JPATH_ADMINISTRATOR, 'en-GB', true);
        $language->load('plg_content_gplusbadge', JPATH_ADMINISTRATOR, null, true);
    }

    // ---------- Joomla 1.6+ methods ------------------

    /**
     * Method is called by the view and the results are imploded and displayed in a placeholder
     *
     * @var string  $context    The context of the content passed to the plugin
     * @var object  $article    content object. Note $article->text is also available
     * @var object  $params     content params
     * @var integer $limitstart The 'page' number
     * @return String
     * @since 1.6
     */
    public function onContentAfterDisplay($context, &$article, &$params, $limitstart=0) {
        if($this->params->def('placement')!='2'){
            return '';
        }
        return $this->buttonScript();
    }

    /**
     * Method is called by the view and the results are imploded and displayed in a placeholder
     *
     * @var string  $context    The context of the content passed to the plugin
     * @var object  $article    content object. Note $article->text is also available
     * @var object  $params     content params
     * @var integer $limitstart The 'page' number
     * @return String
     * @since 1.6
     */
    public function onContentBeforeDisplay($context, &$article, &$params, $limitstart=0) {
        if($this->params->def('placement')!='1'){
            return '';
        }
        return $this->buttonScript();
    }

    /**
     * Method is called by the view
     *
     * @var string  $context    The context of the content passed to the plugin
     * @var object  $article    content object. Note $article->text is also available
     * @var object  $params     content params
     * @var integer $limitstart The 'page' number
     * @return void
     * @since 1.6
     */
    public function onContentPrepare($context, &$article, &$params, $limitstart=0) {
        $this->insertButtonScriptDeclaration();
    }

    // ---------- Joomla 1.5 methods ------------------

    /**
     * The first stage in preparing content for output and is the most common point for content orientated plugins to do their work.
     *
     * @var object  $article    A reference to the article that is being rendered by the view.
     * @var array   $params     A reference to an associative array of relevant parameters.
     * @var integer $limitstart An integer that determines the "page" of the content that is to be generated.
     * @return void
     * @since 1.5
     */
    public function onPrepareContent(&$article, &$params, $limitstart=0) {
        global $mainframe;
        $this->insertButtonScriptDeclaration();
    }

    /**
     * This is a request for information that should be placed immediately before the generated content.
     *
     * @var object  $article    A reference to the article that is being rendered by the view.
     * @var array   $params     A reference to an associative array of relevant parameters.
     * @var integer $limitstart An integer that determines the "page" of the content that is to be generated.
     * @return string           Returned value from this event will be displayed in a placeholder.
     * @since 1.5
     */
    public function onBeforeDisplayContent(&$article, &$params, $limitstart=0) {
        global $mainframe;
        if($this->params->def('placement')!='1'){
            return '';
        }
        return $this->buttonScript();
    }

    /**
     * This is a request for information that should be placed immediately after the generated content.
     *
     * @var object  $article    A reference to the article that is being rendered by the view.
     * @var array   $params     A reference to an associative array of relevant parameters.
     * @var integer $limitstart An integer that determines the "page" of the content that is to be generated.
     * @return string           Returned value from this event will be displayed in a placeholder.
     * @since 1.5
     */
    public function onAfterDisplayContent(&$article, &$params, $limitstart=0) {
        global $mainframe;
        if($this->params->def('placement')!='2'){
            return '';
        }
        return $this->buttonScript();
    }
    
    private function langAsScript(){
        $lang = substr($this->params->def('language'), 0, 2);
        $scriptDec="";
        if($lang!='en'){
            $scriptDec = "window.___gcfg = {lang: '".$lang."'};";
        }
        return $scriptDec;
    }
    private function pageTypeAsAttr(){
        $type = $this->params->def('profile_type')=='2' ? ' rel="publisher"' : ' rel="author"';
        return $type;
    }
    
    private function widthAsAttr(){
        $width = $this->params->def('width')!='' ? ' width="'.$this->params->def('width').'"' : '';
        if($this->params->def('profile_type')=='1'){
            $height='69';
        }else{
            if( $height < '100' ){
                $height = '100';
            }elseif($height > '1024'){
                $height = '1024';
            }
        }
        return $width;
    }
    
    private function heightAsAttr(){
        $height = $this->params->def('height')!='' ? ' height="'.$this->params->def('height').'"' : '';
        if($this->params->def('profile_type')=='1'){
            $height='69';
        }else{
            if( $height < '69' ){
                $height = '69';
            }elseif($height > '131'){
                $height = '131';
            }
        }
        return $height;
    }
    
    private function themeAsAttr(){
        $theme = $this->params->def('theme')=='2' ? ' theme="dark"' : '';
        return $theme;
    }
    
    private function buttonScript(){
        $buttonScript = '<span class="gplusbadge"><g:plus href="https://plus.google.com/'.$this->params->def('profileid').'"'.$this->pageTypeAsAttr().$this->widthAsAttr().$this->heightAsAttr().$this->themeAsAttr().'></g:plus></span>';
        return $buttonScript;
    }
    
    private function scriptDeclaration(){
        $decl= $this->langAsScript().
                '(function(){
                    var po = document.createElement("script");
                    po.type = "text/javascript"; 
                    po.async = true;
                    po.src = "https://apis.google.com/js/plusone.js";
                    var s = document.getElementsByTagName("script")[0];
                    s.parentNode.insertBefore(po, s);
                })();';
        return $decl;
    }
    
    public function insertButtonScriptDeclaration(){
        $document = JFactory::getDocument();
        $document->addScriptDeclaration($this->scriptDeclaration());
    }

}

//End Class